package com.pcbly.web.components.utils.decorator;

import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.module.sitemesh.Config;
import com.opensymphony.module.sitemesh.Decorator;
import com.opensymphony.module.sitemesh.DecoratorMapper;
import com.opensymphony.module.sitemesh.Page;
import com.opensymphony.module.sitemesh.mapper.AbstractDecoratorMapper; 
import com.opensymphony.xwork2.ognl.OgnlValueStack;

@SuppressWarnings("unchecked")
public class ThemeDecoratorMapper extends AbstractDecoratorMapper {
	private static final String IGNORE_PROPERTY_KEY = "decorators-ignore";
	private static String ignore[] = new String[0];

	public ThemeDecoratorMapper() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void init(Config config, Properties properties,
			DecoratorMapper parent) throws InstantiationException {
		String ignoreList = (String) properties.get(IGNORE_PROPERTY_KEY);
		if (ignoreList != null)
			ignore = ignoreList.split(",");
		super.init(config, properties, parent);
	}

	public Decorator getDecorator(HttpServletRequest request, Page page) {
		Decorator decorator = super.getDecorator(request, page);
		if (decorator == null)// ||
								// Boolean.FALSE.toString().equals(JiveGlobals.getLocalProperty("setup"))
								// ||
								// request.getRequestURI().contains("/admin/setup/"))
			return decorator;
		String name = decorator.getName().trim();
		if (!shouldLoad(name))
			return super.getDecorator(request, page);
		List themes = loadTheme(request);
		if (themes != null && themes.size() > 0)
			return createDecorator(themes, decorator);
		else
			return decorator;
	}

	private boolean shouldLoad(String name) {
		boolean loadTheme = true;
		String arr$[] = ignore;
		int len$ = arr$.length;
		int i$ = 0;
		do {
			if (i$ >= len$)
				break;
			String anIgnore = arr$[i$];
			if (anIgnore.trim().equals(name)) {
				loadTheme = false;
				break;
			}
			i$++;
		} while (true);
		return loadTheme;
	}

	private List loadTheme(HttpServletRequest request) {
		OgnlValueStack stack = (OgnlValueStack) request
				.getAttribute("struts.valueStack");
		if (stack == null)
			return null;
		else
			return null;
//			return JiveApplication.getContext().getThemeManager()
//					.determineThemes(new ActionContext(stack.getContext()),
//							request);
	}

	public Decorator getNamedDecorator(HttpServletRequest request, String name) {
		Decorator decorator = super.getNamedDecorator(request, name);
		if (decorator == null || !shouldLoad(decorator.getName().trim()))
			return decorator;
		List themes = loadTheme(request);
		if (themes != null && themes.size() > 0)
			return createDecorator(themes, decorator);
		else
			return decorator;
	}

	private Decorator createDecorator(List themes, Decorator decorator) {
		return new ThemeDecorator(decorator, decorator.getPage(), themes);
	}

}
